# -*- coding: utf-8 -*
from flask import request, jsonify
from flask_login import login_required

from conf.flask_ini import FlaskIni
from conf.my_log import MyLog
from medical_kg.entity_type import EntityType, entity_model
from medical_kg.icd_kg import IcdKg
from medical_kg.med_kg import MedKg
from medical_kg.rel_set_up import RelSetUp
from web.const import StatusCode
from web.const import CONTEXT
from web.validation import valid

app = FlaskIni().get_app()
rbac = FlaskIni().get_rbac()
med_kg_app = MedKg()
icd_kg_app = IcdKg()
entity_type_app = EntityType()

logger = MyLog().get_logger()

prefix = f'{CONTEXT}/rel_set_up'
rel_set_up_app = RelSetUp()


@app.route(f'{prefix}/query_rel_set_up_list', methods=['POST'])
@rbac.allow(roles=['小萌新'], methods=['POST'])
@login_required
def query_rel_set_up_list():
    rel_name = request.json['rel_name']
    start_name = request.json['start_name']
    page_size = request.json['page_size']
    page_num = request.json['page_num']

    valid(rel_name, start_name)

    rel_list = rel_set_up_app.query_rel_list(rel_name, entity_model(start_name),
                                             page_size, page_num)
    total = rel_set_up_app.count_all()
    return jsonify({'status': StatusCode.SUCCESS_CODE,
                    'message': '',
                    'payload': {
                        'rel_list': rel_list,
                        'total': total
                    }})


@app.route(f'{prefix}/create_rel', methods=['POST'])
@rbac.allow(roles=['管理员'], methods=['POST'])
@login_required
def create_rel():
    rel_name = request.json['rel_name']
    start_name = request.json['start_name']
    end_name = request.json['end_name']
    rel_properties_list = request.json['rel_properties_list']
    desc = request.json['desc']

    valid(rel_name, start_name, end_name, desc)

    results = rel_set_up_app.create_rel(rel_name,
                                        entity_model(start_name),
                                        entity_model(end_name), rel_properties_list, desc)
    return jsonify(results)


@app.route(f'{prefix}/update_rel', methods=['POST'])
@rbac.allow(roles=['管理员'], methods=['POST'])
@login_required
def update_rel():
    rel_uid = request.json['rel_uid']
    rel_name = request.json['rel_name']
    start_name = request.json['start_name']
    end_name = request.json['end_name']
    rel_properties_list = request.json['rel_properties_list']
    desc = request.json['desc']

    valid(rel_uid, rel_name, start_name, end_name, desc)

    rel_set_up_app.update_rel(rel_uid, rel_name,
                              entity_model(start_name),
                              entity_model(end_name), rel_properties_list, desc)

    return jsonify({'status': StatusCode.SUCCESS_CODE,
                    'message': ''})


@app.route(f'{prefix}/switch_rel_set_up', methods=['POST'])
@rbac.allow(roles=['管理员'], methods=['POST'])
@login_required
def switch_rel_set_up():
    rel_uid = request.json['rel_uid']
    is_abled = request.json['is_abled']

    valid(rel_uid)

    rel_set_up_app.switch_rel_set_up(rel_uid, is_abled)

    return jsonify({'status': StatusCode.SUCCESS_CODE,
                    'message': ''})
